﻿using System;
using System.Collections.Generic;
using System.Text;
using SMBL.Interface.LifeCycle;

namespace SMBL
{
    namespace Global
    {
        /// <summary>
        /// Class WebSite
        /// Used as a boot sector
        /// @Author: Push
        /// </summary>
        public class WebSite : IStart, IEnd
        {
            //////////////////////////////////////////////////////////////////////////
            // Parameters
            //////////////////////////////////////////////////////////////////////////

            // The Only Instance of the Website
            private static WebSite instanceWebSite;
            // The Config XML file
            private static System.Xml.XmlDocument xmlConfig;

            //////////////////////////////////////////////////////////////////////////
            // Global Attributes
            //////////////////////////////////////////////////////////////////////////

            // Only to get the config file
            public static System.Xml.XmlDocument ConfigXML
            {
                get { return xmlConfig; }
            }

            // Application Path
            public static readonly String AppPath = AppDomain.CurrentDomain.BaseDirectory;
            // System Path
            public static readonly String SysPath = AppPath + "_System\\";
            // Config File Path
            public static readonly String ConfigPath = WebSite.SysPath + "Config.xml";

            //////////////////////////////////////////////////////////////////////////
            // Interface
            //////////////////////////////////////////////////////////////////////////

            /// <summary>
            /// IStart:Start
            /// @Author: Push
            /// Start BasicErrorSystem
            /// Load the system's config file
            /// Initialize global parameters
            /// </summary>
            /// <returns>Statue of the process</returns>
            #region IStart Members

            public bool Start()
            {
                //throw new NotImplementedException();
                WebSite.xmlConfig = new System.Xml.XmlDocument();
                // Load the config file.
                // Throw exception when: 
                // 1. File Not found
                // 2. Path too long
                // 3. Others
                xmlConfig.Load(WebSite.ConfigPath);

                return true;
            }

            #endregion

            /// <summary>
            /// IEnd:End
            /// @Author: Push
            /// Release the global resource
            /// End BasicErrorSystem
            /// </summary>
            /// <returns></returns>
            #region IEnd Members

            public bool End()
            {
                //throw new NotImplementedException();

                // Save the config file
                // if it is not able to write the file
                // throw the exception.
                WebSite.xmlConfig.Save(WebSite.ConfigPath);
                return true;
            }

            #endregion

            //////////////////////////////////////////////////////////////////////////
            // Static Global Method
            //////////////////////////////////////////////////////////////////////////

            /// <summary>
            /// Boot the website
            /// @Author: Push
            /// Create an instance of the website
            /// Start it.
            /// </summary>
            public static void Boot()
            {
                BasicErrorSystem.Initialize();

                try
                {
                    // Check the instance statue
                    // End the website first
                    if (WebSite.instanceWebSite != null)
                        WebSite.instanceWebSite.End();

                    instanceWebSite = new WebSite();
                    // Start the website
                    instanceWebSite.Start();
                }
                catch (System.Exception e)
                {
                    // Handle the exception
                    BasicErrorSystem.UnhandledError(e);
                }
            }

            /// <summary>
            /// Shutdown the website
            /// @Author: Push
            /// </summary>
            public static void Shutdown()
            {
                try
                {
                    // Unstarted Website
                    if (instanceWebSite == null) return;

                    instanceWebSite.End();
                }
                catch (System.Exception e)
                {
                    // Handle the exception
                    BasicErrorSystem.UnhandledError(e);
                }
            }
        }
    }
}